package com.xnxy.design.action;

import com.xnxy.design.model.OrderForm;
import com.xnxy.design.model.User;
import com.xnxy.design.service.CoreService;
import com.xnxy.design.service.GoodsService;
import com.xnxy.design.service.OrderFormService;
import com.xnxy.design.service.UserService;
import com.xnxy.design.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/order")
@Controller
public class OrderFormAction extends CoreController<OrderForm,Integer>{

    @Autowired
    OrderFormService orderFormService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    UserService userService;

    @Override
    public CoreService<OrderForm, Integer> getService() {
        return orderFormService;
    }


    @RequestMapping("/admin/delect")
    @ResponseBody
    public Map<String,Object> delectOrder(Integer id[]){
        Boolean flag=delect(id);
        if (flag){
            return success(true,"删除成功");
        }else {
            return success(false,"删除失败");
        }

    }

    @RequestMapping("/admin/getOrder")
    @ResponseBody
    public Map<String,Object> getOrder(Integer id){
        OrderForm orderForm=selectByPrimaryKey(id);
        return success(true,"请求成功",orderForm);
    }


    @RequestMapping("/admin/list")
    @ResponseBody
    public Map<String,Object> list(){
        List list=selectAll();
        return success(true,"查询成功",list);
    }

    @RequestMapping("/admin/updata")
    @ResponseBody
    public Map<String,Object> updataOrder(OrderForm orderForm , MultipartFile file) throws IOException {
        if (file!=null){
            String fileName = System.currentTimeMillis() + file.getOriginalFilename();
            String destFileName = SessionUtil.gerReq().getServletContext().getRealPath("") + "static" + File.separator + fileName;
            File destFile = new File(destFileName);
            destFile.getParentFile().mkdirs();
            //5.把浏览器上传的文件复制到希望的位置
            file.transferTo(destFile);
            orderForm.setImage("static/"+fileName);
        }
        if (orderForm.getId()!=null){
            orderFormService.updata(orderForm);
        }
        return success(true,"请求成功");
    }


    @RequestMapping("/inster")
    public ModelAndView orderAction(OrderForm orderForm){
        ModelAndView modelAndView=new ModelAndView("ry");
        orderForm.setUserid(SessionUtil.getUser().getId());
        orderForm.setType(0);
        orderFormService.insert(orderForm);
        modelAndView.addObject("type",2);
        modelAndView.addObject("orderForm",orderForm);
        modelAndView.addObject("goods",goodsService.selectByPrimaryKey(orderForm.getGid()));
        return modelAndView;
    }

    @RequestMapping("/jixuzhifu")
    public ModelAndView jixuzhifu(Integer id){
        OrderForm orderForm=orderFormService.selectByPrimaryKey(id);
        ModelAndView modelAndView=new ModelAndView("/ry");
        modelAndView.addObject("type",2);
        modelAndView.addObject("orderForm",orderForm);
        modelAndView.addObject("goods",goodsService.selectByPrimaryKey(orderForm.getGid()));
        return modelAndView;
    }

    @RequestMapping("/zf")
    public ModelAndView zf(Integer oid,Integer gtime,String zfpassword){
        OrderForm orderForm =orderFormService.selectByPrimaryKey(oid);
        if (!SessionUtil.getUser().getZfpassword().equals(zfpassword)){
            ModelAndView modelAndView=new ModelAndView("ry");
            modelAndView.addObject("type",2);
            modelAndView.addObject("orderForm",orderForm);
            modelAndView.addObject("goods",goodsService.selectByPrimaryKey(orderForm.getGid()));
            modelAndView.addObject("iszf","密码不正确！");
            return modelAndView;
        }
        orderForm.setStartdate(new Date());
        orderForm.setType(1);
        orderForm.setEnddate(new Date(new Date().getTime() + 60*60*1000*24*gtime));

        User user=SessionUtil.getUser();
        if (user.getBalance()==null || user.getBalance()<orderForm.getRnumber()){
            ModelAndView modelAndView=new ModelAndView("ry");
            modelAndView.addObject("type",2);
            modelAndView.addObject("orderForm",orderForm);
            modelAndView.addObject("goods",goodsService.selectByPrimaryKey(orderForm.getGid()));
            modelAndView.addObject("iszf","余额不足！");
            return modelAndView;
        }else {
            user.setBalance(user.getBalance()-orderForm.getRnumber());
            userService.updata(user);
            orderFormService.updata(orderForm);
        }
        ModelAndView modelAndView=new ModelAndView("ry");
        modelAndView.addObject("type",3);
        return modelAndView;
    }


    @RequestMapping("/isUserAdmin")
    private ModelAndView isUserAdmin(Integer type) {
        ModelAndView modelAndView = new ModelAndView("userAdmin");
        modelAndView.addObject("type",type);
        List<OrderForm> list=orderFormService.selectAll();
        List<OrderForm> orders=new ArrayList<>();
        for(OrderForm orderForm:list){
            if (orderForm.getUserid()==SessionUtil.getUser().getId()&&orderForm.getType()!=4){
                orders.add(orderForm);
            }
        }
        modelAndView.addObject("orders",orders);
        return modelAndView;
    }

    @RequestMapping("/orderwancheng")
    private ModelAndView orderwancheng(Integer type) {
        ModelAndView modelAndView = new ModelAndView("userAdmin");
        modelAndView.addObject("type",type);
        List<OrderForm> list=orderFormService.selectAll();
        List<OrderForm> orders=new ArrayList<>();
        for(OrderForm orderForm:list){
            if (orderForm.getUserid()==SessionUtil.getUser().getId()&&orderForm.getType()==4){
                orders.add(orderForm);
            }
        }
        modelAndView.addObject("orders",orders);
        return modelAndView;
    }
}
